home *** CD-ROM | disk | FTP | other *** search
File List | 1986-10-19 | 2.3 KB | 118 lines |
- ' *******************
- ' *** SORTINT.LST ***
- ' *******************
- '
- DEFWRD "a-z"
- '
- > PROCEDURE quick.sort.int(VAR proc%())
- ' *** sort integer array by recursive Quick Sort
- LOCAL last,dummy%
- last=DIM?(proc%())-1
- @quick.int(1,last)
- RETURN
- ' ***
- > PROCEDURE quick.int(l,r)
- LOCAL ll,rr,i,x,j
- ll=l
- rr=r
- dummy%=proc%(DIV(ADD(l,r),2))
- REPEAT
- WHILE proc%(l)<dummy%
- INC l
- WEND
- WHILE proc%(r)>dummy%
- DEC r
- WEND
- IF l<=r
- SWAP proc%(l),proc%(r)
- INC l
- DEC r
- ENDIF
- UNTIL l>r
- IF ll<r
- @quick.int(ll,r)
- ENDIF
- IF l<rr
- @quick.int(l,rr)
- ENDIF
- RETURN
- ' **********
- '
- > PROCEDURE shell.sort.int(VAR proc%())
- ' *** sort integer array by Shell Sort
- LOCAL inc,last,j,k,inserted!,x%,current,previous
- last=DIM?(proc%())-1
- LET inc=last
- WHILE inc>1
- DIV inc,2
- FOR j=1 TO inc
- k=ADD(j,inc)
- WHILE k<=last
- inserted!=FALSE
- x%=proc%(k)
- current=k
- previous=SUB(current,inc)
- WHILE previous>=j AND NOT inserted!
- IF x%<proc%(previous)
- proc%(current)=proc%(previous)
- current=previous
- SUB previous,inc
- ELSE
- inserted!=TRUE
- ENDIF
- WEND
- proc%(current)=x%
- ADD k,inc
- WEND
- NEXT j
- WEND
- RETURN
- ' **********
- '
- > PROCEDURE bin.search.int(element%,VAR proc%(),index)
- ' *** find element% in sorted integer array (binary search)
- ' *** global : FOUND!
- LOCAL first,last,middle
- first=1
- last=DIM?(proc%())-1
- WHILE first<last
- middle=DIV(ADD(first,last),2)
- IF element%>proc%(middle)
- first=ADD(middle,1)
- ELSE
- last=middle
- ENDIF
- WEND
- found!=(proc%(first)=element%)
- IF found!
- index=first
- ELSE
- index=0
- ENDIF
- RETURN
- ' **********
- '
- > PROCEDURE bin.search.word(element,VAR proc(),index)
- ' *** find element in sorted word array (binary search)
- ' *** global : FOUND!
- LOCAL first,last,middle
- first=1
- last=DIM?(proc())-1
- WHILE first<last
- middle=DIV(ADD(first,last),2)
- IF element>proc(middle)
- first=ADD(middle,1)
- ELSE
- last=middle
- ENDIF
- WEND
- found!=(proc(first)=element)
- IF found!
- index=first
- ELSE
- index=0
- ENDIF
- RETURN
- ' **********
- '
-